#include "StdAfx.h"
#include ".\warp.h"

CWarp::CWarp(void)
{
	Init();
}

CWarp::~CWarp(void)
{
	Release();
}


void CWarp::Init(void)
{
	this->m_pSrcImage1 = NULL;
	this->m_pSrcImage2 = NULL;

	this->m_pDestImage1 = NULL;
	this->m_pDestImage2 = NULL;

	this->m_SrcImageWindowName1 = "Image1";
	this->m_SrcImageWindowName2 = "Image2";

	this->m_DestImageWindowName1 = "Warped1";
	this->m_DestImageWindowName2 = "Warped2";
}

int CWarp::LoadImage1(char* pszFileName_)
{
	ReleaseSrcImage1();
	this->m_pSrcImage1 = cvLoadImage(pszFileName_, -1);
	if (this->m_pSrcImage1 == NULL) {
		return -1;
	}
	cvNamedWindow(this->m_SrcImageWindowName1, 1);
	cvShowImage(this->m_SrcImageWindowName1,this->m_pSrcImage1);
	return 0;
}

int CWarp::LoadImage2(char* pszFileName_)
{
	ReleaseSrcImage2();
	this->m_pSrcImage2 = cvLoadImage(pszFileName_, -1);
	if (this->m_pSrcImage2  == NULL) {
		return -1;
	}
	cvNamedWindow(this->m_SrcImageWindowName2, 1);
	cvShowImage(this->m_SrcImageWindowName2,this->m_pSrcImage2);
	return 0;
}

void CWarp::ReleaseSrcImage1(void)
{
	if(NULL != this->m_pSrcImage1){
		cvReleaseImage(&this->m_pSrcImage1);
		m_pSrcImage1 = NULL;
		cvDestroyWindow(this->m_SrcImageWindowName1);
	}

}
void CWarp::ReleaseSrcImage2(void)
{
	if(NULL != this->m_pSrcImage2){
		cvReleaseImage(&this->m_pSrcImage2);
		m_pSrcImage2 = NULL;
		cvDestroyWindow(this->m_SrcImageWindowName2);
	}

}


void CWarp::ReleaseDestImage1(void)
{
	if(NULL != this->m_pDestImage1){
		cvReleaseImage(&this->m_pDestImage1);
		m_pDestImage1 = NULL;
		cvDestroyWindow(this->m_DestImageWindowName1);
	}

}

void CWarp::ReleaseDestImage2(void)
{

	if(NULL != this->m_pDestImage2){
		cvReleaseImage(&this->m_pDestImage2);
		m_pDestImage2 = NULL;
		cvDestroyWindow(this->m_DestImageWindowName2);
	}
}

void CWarp::Release(void)
{
	ReleaseSrcImage1();
	ReleaseSrcImage2();
	ReleaseDestImage1();
	ReleaseDestImage2();
}

int CWarp::MakeWarpImage(void)
{
	ReleaseDestImage1();
	ReleaseDestImage2();
	
	if(NULL == this->m_pSrcImage1 || NULL == this->m_pSrcImage2){
		return -1;
	}

	/*void cvWarpPerspective( const CvArr* src, CvArr* dst, const CvMat* map_matrix,
                        int flags=CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS,
                        CvScalar fillval=cvScalarAll(0) );
*/
	this->m_pDestImage1 = cvCreateImage(cvGetSize(this->m_pSrcImage1), IPL_DEPTH_8U, 3);
	this->m_pDestImage2 = cvCreateImage(cvGetSize(this->m_pSrcImage2), IPL_DEPTH_8U, 3);

	CvMat *pMat1 = cvCreateMat( 3, 3, CV_32FC1);
	cvmSet(pMat1,0,0, 0.000002);
	cvmSet(pMat1,0,1, 0.000041);
	cvmSet(pMat1,0,2, -0.016760);

	cvmSet(pMat1,1,0, -0.000038);
	cvmSet(pMat1,1,1, 0.000004);
	cvmSet(pMat1,1,2, 0.004283);

	cvmSet(pMat1,2,0, 0.013504);
	cvmSet(pMat1,2,1, -0.007652);
	cvmSet(pMat1,2,2, 1.000000);

	CvMat *pMat2 = cvCreateMat( 3, 3, CV_32FC1);
	cvmSet(pMat2,0,0, 0.857612);
	cvmSet(pMat2,0,1, -0.098031);
	cvmSet(pMat2,0,2, 53.685394 );

	cvmSet(pMat2,1,0, -0.040177);
	cvmSet(pMat2,1,1, 0.974213);
	cvmSet(pMat2,1,2, -31.685696 );

	cvmSet(pMat2,2,0, -0.000226);
	cvmSet(pMat2,2,1, 0.000014);
	cvmSet(pMat2,2,2, 1.000000);

	//cvWarpPerspective(m_pSrcImage1,m_pDestImage1,pMat1,CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS,CV_RGB(255,0,0) );
	cvWarpPerspective(m_pSrcImage1,m_pDestImage1,pMat2,CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS,CV_RGB(255,0,0) );
	cvNamedWindow(this->m_DestImageWindowName1, 1);
	cvShowImage(this->m_DestImageWindowName1,this->m_pDestImage1);



	cvWarpPerspective(m_pSrcImage2,m_pDestImage2,pMat2 );
	cvNamedWindow(this->m_DestImageWindowName2, 1);
	cvShowImage(this->m_DestImageWindowName2,this->m_pDestImage2);
	
	return 0;
}